package util;

import businessLogic.ManagerBeanLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import model.League;

public class LeagueConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return lookupManagerBean().getLeagueWithId(value);
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return String.valueOf(((League) value).getLeagueId());
    }

    private ManagerBeanLocal lookupManagerBean() {
        try {
            Context c = new InitialContext();
            return (ManagerBeanLocal) c.lookup("java:comp/env/ManagerBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
